#!/usr/bin/env python
import sys
from FoldSegment import *

class FoldTree:

	def __init__( self, FoldingMargin ):
		self.List          = []
		self.FoldingMargin = FoldingMargin
		self.TextEditor    = FoldingMargin.TextEditor
	
	def LineStartsWithKeyword( self, line ):
		keywords  = [ "def", "class", "if", "for", "else", "elif" ]
		ret       = False
		for key in keywords:
			if line.strip().startswith( key ):
				ret = True
			 	break
		return ret
			
	#functia care parsuieste textul si construieste punctele de folding
	def MakeList( self, text ):
		self.List   = []
		hidden_tag  = self.TextEditor.TextBuffer.get_tag_table().lookup("hidden")
		folded_tag  = self.TextEditor.TextBuffer.get_tag_table().lookup("fold-title")
		old_tabs_nr = 0
		lines       = text.split("\n")
		for i in range( 0, len(lines) ):
			line = lines[i]
			if self.LineStartsWithKeyword( line ):
				#nr taburile
				segment               = FoldSegment( self )
				segment.LineTabSpaces = line.count("\t") + 1
				#
				segment.StartLineIter = self.TextEditor.TextBuffer.get_iter_at_line( i )
				#daca este titlu de fold, pune-l sa fie collapsed
				if segment.StartLineIter.has_tag( folded_tag ): segment.Collapsed = True
				#nu adauga si ce este in portiunea ascunsa
				if not ( segment.StartLineIter.has_tag( hidden_tag ) ):
					self.List.append( segment )
	
